//
//  YWInspectionTimeLineCell.m
//  maintain
//
//  Created by Kyo on 16/7/15.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWInspectionTimeLineCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "YYLabel.h"

#import "YWInspectionMissionManager.h"

@interface YWInspectionTimeLineCell ()
@property (nonatomic, strong) UIView *topLineView;
@property (nonatomic, strong) UIView *bottomLineView;
@property (nonatomic, strong) YYLabel *statusLabel;
@property (nonatomic, strong) YYLabel *contentLabel;
@property (nonatomic, strong) YYLabel *timeLabel;
@end

@implementation YWInspectionTimeLineCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    YWInspectionTimeLineCell *cell = [tableView dequeueReusableCellWithIdentifier:inspectionTimeLineCellIdentifier];
    if (cell == nil) {
        cell = [[YWInspectionTimeLineCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:inspectionTimeLineCellIdentifier];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.separatorInset = UIEdgeInsetsMake(0, 30, 0, 0);
    }
    return self;
}

-(void)setDicData:(NSDictionary *)dicData{
    _dicData = dicData;
    YWInspectionEquipmentStatusType type = [[dicData objectForKey:YWMissionManagerDicStatusType]integerValue];
    if (YWInspectionEquipmentStatusTypeUnfinish == type) { //灰
        self.statusLabel.backgroundColor = [UIColor customTextGrayColor];
    }else if (YWInspectionEquipmentStatusTypeFinishNormal == type){ //绿
        self.statusLabel.backgroundColor = [UIColor customGreenColor];
    }else if (YWInspectionEquipmentStatusTypeFinishTrouble == type){//红
        self.statusLabel.backgroundColor = [UIColor customRedColor];
    }
    NSString *content = [dicData objectForKey:YWMissionManagerDicContent];
    NSString *locaton = [dicData objectForKey:YWMissionManagerDicLocation];
    if (content.length>0 && locaton.length>0) {
        self.contentLabel.text = [NSString stringWithFormat:@"%@\n%@",content?:@"",locaton?:@""];
    }else{
        self.contentLabel.text = content.length>0?content:(locaton.length?locaton:NSLocalizedString(@"unknowLocation", nil));
    }
    
    NSString *time = [dicData objectForKey:YWMissionManagerDicTime];
    self.timeLabel.text = time.length>0?time:@"";
    
    self.contentLabel.preferredMaxLayoutWidth = self.contentLabel.frame.size.width;
}

- (void)setUpSubviews{
    CGFloat statusSize = 14;
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.contentView.mas_top).offset(10);
        make.left.mas_equalTo(self.statusLabel.mas_right).offset(10);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-10);
        make.bottom.mas_equalTo(self.timeLabel.mas_top).offset(-3);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.mas_equalTo(self.contentLabel.mas_left);
        make.right.mas_equalTo(self.contentLabel.mas_right);
//        make.height.mas_equalTo(kMinLabelH);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-10);
    }];
    
    [self.statusLabel mas_makeConstraints:^(MASConstraintMaker* make) {
//        make.top.mas_equalTo(self.contentLabel.mas_top);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.left.mas_equalTo(self.contentView.mas_left).offset(10);
        make.width.mas_equalTo(statusSize);
        make.height.mas_equalTo(statusSize);
    }];
    [self.topLineView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.contentView.mas_top);
        make.bottom.mas_equalTo(self.statusLabel.mas_top);
        make.centerX.mas_equalTo(self.statusLabel.mas_centerX);
        make.width.mas_equalTo(1);
    }];
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.statusLabel.mas_bottom);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
        make.centerX.mas_equalTo(self.statusLabel.mas_centerX);
        make.width.mas_equalTo(1);
    }];
}

//- (void)layoutSubviews{
//    [super layoutSubviews];
//   
//}

- (UIView *)topLineView{
    if (!_topLineView) {
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor customLineColor];
        _topLineView = view;
        [self.contentView addSubview:_topLineView];
    }
    return _topLineView;
}

- (UIView *)bottomLineView{
    if (!_bottomLineView) {
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor customLineColor];
        _bottomLineView = view;
        [self.contentView addSubview:_bottomLineView];
    }
    return _bottomLineView;
}

- (YYLabel *)statusLabel{
    if (!_statusLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.backgroundColor = [UIColor customTextGrayColor];
        label.layer.masksToBounds = YES;
        label.layer.cornerRadius = 14/2;
        _statusLabel = label;
        [self.contentView addSubview:_statusLabel];
    }
    return _statusLabel;
}

- (YYLabel *)contentLabel{
    if (!_contentLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        label.font = KSmallContentFont;
        label.textColor = [UIColor blackColor];
        _contentLabel = label;
        [self.contentView addSubview:_contentLabel];
    }
    return _contentLabel;
}

- (YYLabel *)timeLabel{
    if (!_timeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = KSmallContentFont;
        label.textColor = [UIColor customTextGrayColor];
        _timeLabel = label;
        [self.contentView addSubview:_timeLabel];
    }
    return _timeLabel;
}
@end
